package com.demo.interceptor;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;

/**
 * BlogInterceptor
 */
public class UserInterceptor implements Interceptor {
	
	public void intercept(ActionInvocation ai) {
		System.out.println("Before invoking " + ai.getActionKey());
		Controller contrl = ai.getController();
		String attr = contrl.getSessionAttr("username");
		StringBuffer returnUrl = contrl.getRequest().getRequestURL();
		if(StringUtils.isBlank(attr)){
			contrl.redirect("/blog/tologin?returnUrl="+returnUrl);
		}else{
			ai.invoke();
		}	
		System.out.println("After invoking " + ai.getActionKey());
	}
}
